﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Obligatorio.DataAccess;

public partial class SiteMaster : System.Web.UI.MasterPage, IMasterPageView
{
    #region Fields
    MasterPagePresenter _presenter;
    List<Role> _roles;
    #endregion

    #region Properties
    public bool IsAuthenticated
    {
        get { return !string.IsNullOrEmpty(CookieManager.GetValue(CommonValues.USER_ID_KEY)); }
    }

    public long UserId
    {
        get { return Convert.ToInt64(CookieManager.GetValue(CommonValues.USER_ID_KEY)); }
    }

    public List<Role> Roles
    {
        set
        {
            _roles = value;
        }
    }

    public string UserName
    {
        get { return CookieManager.GetValue(CommonValues.USERNAME_KEY); }
    }
    #endregion


    protected override void OnInit(EventArgs e)
    {
        _presenter = new MasterPagePresenter(this);
        base.OnInit(e);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        _presenter.Load();
    }

    public void PopulateMenu()
    {
        foreach (var role in _roles)
        {
            this.NavigationMenu.Items.Add(new MenuItem(role.RoleName, null, null, role.RoleModuleUrl));
        }
    }


    protected void NavigationMenu_MenuItemDataBound(object sender, MenuEventArgs e)
    {
        Role r = (e.Item.DataItem as Role);

        if (r != null && !string.IsNullOrEmpty(r.RoleModuleUrl))
        {
            e.Item.Text = r.RoleName;
            e.Item.NavigateUrl = r.RoleModuleUrl;
        }
    }

    protected void HeadLoginStatus_LoggingOut(Object sender, System.Web.UI.WebControls.LoginCancelEventArgs e)
    {
        CookieManager.DeleteCookie();
    }
}
